package it.easycrm.data.dao.jpa;

import it.easycrm.data.dao.interfaces.CustomerDao;
import it.easycrm.data.dataobjects.Customer;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import javax.persistence.TypedQuery;

import org.springframework.orm.jpa.JpaCallback;
/**
 * 
 * @author Francesco De Simone
 *
 */
public class CustomerDaoImpl extends AbstractDaoJPAImpl<Customer> implements  CustomerDao {

	public CustomerDaoImpl() {
		super(Customer.class);
	}
	public CustomerDaoImpl(Class<Customer> domainClass) {
		super(Customer.class);
		
	}

	public List<Customer> findAll() {
		return getJpaTemplate().execute(new JpaCallback<List<Customer>>() {
			public List<Customer> doInJpa(EntityManager em) throws PersistenceException {
				TypedQuery<Customer> query = em.createQuery("select e from Customer e", Customer.class);
				return query.getResultList();
			}
		});
	}

	public int countAll() {
		return getJpaTemplate().execute(new JpaCallback<Integer>() {

			public Integer doInJpa(EntityManager em) throws PersistenceException {
				TypedQuery<Long> query = em.createQuery("select count (e) from Customer e", Long.class);
				return (query.getSingleResult()).intValue();
			}
		});
	}

}
